﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.BussnessLogicLayer;
using Capstone_project.DatabaseAccessLayer;

namespace Capstone_project
{
    public partial class frmAddNewCustomer : Form
    {
        CustomerBUS customerbus = new CustomerBUS();
        DataTable dt = new DataTable();
        SqlDataAdapter adap=new SqlDataAdapter();
        BindingSource bdsource;
        public frmAddNewCustomer()
        {
            InitializeComponent();
        }

        private void frmAddNewCustomer_Load(object sender, EventArgs e)
        {
            string sql = "SELECT * FROM CUSTOMER";
            SqlConnection con = Common.con;
            if(con.State!=ConnectionState.Open)
                con.Open();
            SqlCommand com = new SqlCommand(sql,con);
            adap = new SqlDataAdapter(com);
            adap.Fill(dt);
            con.Close();
            bdsource = new BindingSource();
            bdsource.DataSource = dt;
            bindingNavigator1.BindingSource = bdsource;
            dataGridView1.DataSource = bdsource;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            txtCustomerID.Text = customerbus.nextID();
            txtCustomerID.Enabled = false;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "INSERT INTO CUSTOMER (ID_CUSTOMER,NAME_CUSTOMER,ADDRESS,PHONE) VALUES ('" + txtCustomerID.Text + "','" + txtCustomerName.Text + "','" + txtCustomerAddress.Text + "','" + txtCustomerPhone.Text + "')";
                SqlConnection con = Common.con;
                con.Open();
                SqlCommand com = new SqlCommand(sql, con);
                int kq = com.ExecuteNonQuery();
                if (kq != 0)
                {
                    MessageBox.Show("Create new customer successful");
                }
            }
            catch
            {
                MessageBox.Show("Fail to create new customer");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to save changes ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    
                    SqlCommandBuilder s = new SqlCommandBuilder(adap);
                    int kq = adap.Update(dt);
                    dataGridView1.DataSource = dt;
                    if (kq != 0)
                    {
                        MessageBox.Show("Changes to be saved");
                        dataGridView1.Update();
                    }
                }
                catch
                {
                    MessageBox.Show("Fail to save");
                    dataGridView1.Refresh();
                }
            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            DataRow dr = dt.NewRow();
            dr["ID_CUSTOMER"] = customerbus.nextID();
            dr["NAME_CUSTOMER"] = "";
            dr["ADDRESS"] = "";
            dr["PHONE"] = "";
            dt.Rows.Add(dr);
            //bindingNavigator1.BindingSource.MoveLast();
            //int last = dataGridView1.Rows.Count;
            //dataGridView1.CurrentCell = dataGridView1.Rows[last - 2].Cells[0];
            //dataGridView1.CurrentCell.Selected = true;
        }
    }
}
